package edu.xjtu.jih.selfDemands;

import edu.xjtu.jih.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class SelfDemandsService {
    public List<SelfDemands> getSelfDemandsList() {

        SqlSession sess= Utils.openSession();

        try{
            SelfDemandsDao dao=sess.getMapper(SelfDemandsDao.class);
            List<SelfDemands> selfDemandsList=dao.findSelfDemandsList();
            sess.commit();
            return selfDemandsList;

        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("获取信息失败",e);
        }finally{
        sess.close();
        }

    }

    /*public void addItem(SelfDemandsDto dto) {

        SqlSession sess= Utils.openSession();

        try{
            SelfDemandsDao dao=sess.getMapper(SelfDemandsDao.class);
            dao.insertItem(dto);
            sess.commit();


        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("获取信息失败",e);
        }finally{
            sess.close();
        }

    }*/

    public void updateSelfDemandsInfo(SelfDemandsDto dto) {
        SqlSession sess = Utils.openSession();
        try{
            SelfDemandsDao dao = sess.getMapper(SelfDemandsDao.class);
            dao.updateSelfDemands(dto);
            sess.commit();

        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("修改商品信息失败！",e);
        }finally {
            sess.close();
        }

    }

    public void deleteSelfDemandsInfo(SelfDemandsDto dto) {
        SqlSession sess = Utils.openSession();
        try{
            SelfDemandsDao dao = sess.getMapper(SelfDemandsDao.class);
            dao.deleteSelfDemands(dto.getItem_id());
            sess.commit();

        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("删除商品信息失败！",e);
        }finally {
            sess.close();
        }

    }
}
